﻿using ClientServer.Utils;
using server.DAO;
using server.Model;
using server.Tool;
using ServerFramework.Tool.Singleton;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.Business
{
    /// <summary>
    /// 处理客户端的背包信息
    /// </summary>
    public class KnapsackHandle :Singleton<KnapsackHandle>
    {
        private CURDRoomDao curdRoomDao = new CURDRoomDao();
        private SearchGrounpDao searchGrounpDao = new SearchGrounpDao();
        private KnapsackDao knapsackDao = new KnapsackDao();

        /// <summary>
        /// 发送背包物资信息给客户端
        /// </summary>

        public void RealPushKnapsackData()
        {
            ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
            foreach (PubgSession session in dic.Keys)
            {
                SessionItem sessionItem = null;
                dic.TryGetValue(session, out sessionItem);
                if (sessionItem != null && !string.IsNullOrEmpty(sessionItem.gpsItem.userName) && sessionItem.isLogin)
                {
                    Grounp grounp = null;
                    int roomId = 0;
                    //玩家
                    if (sessionItem.gpsItem.userType == 0)
                    {

                        roomId = MapDataPushHandle.Instance.GetRoomByUser(sessionItem.gpsItem.userId);
                        if(roomId==-1)
                        {
                            continue;
                        }
                        grounp = curdRoomDao.GetGrounpByRoomId(roomId);
                    }
                    //管理端
                    else
                    {
                        grounp = searchGrounpDao.GetGrounpByAdmin(sessionItem.gpsItem.userId);
                    }
                    if(grounp==null)
                    {
                        continue;
                    }
                    //游戏未运行状态则不进行推送
                    if(grounp.runState==-1)
                    {
                        continue;
                    }
                    //处理过时的
                    knapsackDao.DeleteOutOfDateKnapsack(grounp);
                    //根据最近时间内的包信息推送到前台
                    List<Knapsack_Detail> all_knapsack_Detail_List = knapsackDao.GetTimeInKnapsack(grounp);
                    if(all_knapsack_Detail_List.Count==0)
                    {
                        session.Send("ShowKnapsackInfo" + Constant.START_SPLIT + "clear" + "\r\n");
                    }
                    else
                    {
                        List<Knapsack_Detail_Item> knapsack_Detail_ItemList = new List<Knapsack_Detail_Item>();
                        foreach (Knapsack_Detail item in all_knapsack_Detail_List)
                        {
                            float distance = -1;
                            //玩家端增加距离
                            if (sessionItem.gpsItem.userType == 0)
                            {
                                distance = GISUtils.CalcDistance(sessionItem.gpsItem.lon, sessionItem.gpsItem.lat, item.lon, item.lat);
                            }
                            //大于1公里的化不显示
                            if(Math.Abs(distance)>1000)
                            {
                                continue;
                            }
                            Knapsack_Detail_Item push_Deta_Item = new Knapsack_Detail_Item()
                            {
                                knapsack_Detail = item,
                                distance = Convert.ToInt16(distance)
                            };
                            knapsack_Detail_ItemList.Add(push_Deta_Item);

                        }
                        //玩家最近距离的三个装备
                        List<Knapsack_Detail_Item> knapsack_Detail_Item_Result = null;
                        if (sessionItem.gpsItem.userType == 0)
                        {
                            knapsack_Detail_Item_Result = knapsack_Detail_ItemList.OrderBy(t => t.distance).Take(3).ToList<Knapsack_Detail_Item>();

                        }
                        //管理端全部显示
                        else
                        {
                            knapsack_Detail_Item_Result = knapsack_Detail_ItemList;
                        }
                        Dictionary<string, object> dataDic = new Dictionary<string, object>();
                        dataDic.Add("knapsackInfo", knapsack_Detail_Item_Result);
                        string resultJson = Utils.CollectionsConvert.ToJSON(dataDic);
                        string data = "ShowKnapsackInfo" + Constant.START_SPLIT + resultJson + "\r\n";
                        session.Send(data);

                       // Console.WriteLine(sessionItem.gpsItem.userName + "==knapsack info :" + knapsack_Detail_Item_Result.Count);
                    }

                   
                }
            }


        }
        private static int catchDistance = 10;
        /// <summary>
        /// 捕捉物资 ，在定时器中触发
        /// </summary>
        public void CatchKnapsack()
        {
            ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
            foreach (PubgSession session in dic.Keys)
            {
                SessionItem sessionItem = null;
                dic.TryGetValue(session, out sessionItem);
                if (sessionItem != null && !string.IsNullOrEmpty(sessionItem.gpsItem.userName) && sessionItem.gpsItem.lat > 1)
                {

                    if (sessionItem.gpsItem.userType == 1)
                    {
                        continue;
                    }
                    Grounp grounp = null;
                    int roomId = 0;
                    if (sessionItem.gpsItem.userType == 0)
                    {

                        roomId = MapDataPushHandle.Instance.GetRoomByUser(sessionItem.gpsItem.userId);
                        grounp = curdRoomDao.GetGrounpByRoomId(roomId);
                    }
                    else
                    {

                        grounp = searchGrounpDao.GetGrounpByAdmin(sessionItem.gpsItem.userId);
                    }

                 
                    List<Knapsack_Detail> knapsack_Detail_List = knapsackDao.GetTimeInKnapsack(grounp);
                    for(int i=0;i< knapsack_Detail_List.Count;i++)
                    {
                        int distance = (int)GISUtils.CalcDistance(sessionItem.gpsItem.lon, sessionItem.gpsItem.lat,
                            knapsack_Detail_List[i].lon, knapsack_Detail_List[i].lat);
                        if (distance <= catchDistance)
                        {
                            UserKnapsack userKnapsack = new UserKnapsack();
                            userKnapsack.userId = int.Parse(sessionItem.userId);
                            userKnapsack.knapsackDetail = knapsack_Detail_List[i];
                            knapsackDao.LocateKnapsackDetailConfirm(userKnapsack);

                            //无敌信息确认
                            if(knapsack_Detail_List[i].knapsack.type==2)
                            {
                                string data = "InvincibleKnapsackInfo" + Constant.START_SPLIT + knapsack_Detail_List[i].addCountValue + "\r\n";
                                session.Send(data);
                            }
                            break;
                        }
                    }

                   
                }
            }
        }
    }
}
